import axios, {AxiosRequestConfig} from "axios"
import {useUserInfo} from "../store/moduls/userinfo";
import {Ref} from "vue";
import {useLayout} from "../store/moduls/layout";



interface loadingtype {
    [prop: string]: Ref<boolean>
}

const loadingstorage: loadingtype = {} // 所有加载状态的仓库，key是url，value是ref数据，保证这边修改，那边同步修改

const excepturl:Array<string> = []        // 不需要防抖的url数组
let antiShakeurlList:Array<string> = [] // 被缓存的url数组

const BaseUrl = import.meta.env.VITE_RES_URL || "/proxy/"
const http = axios.create({
    // 联调
    // baseURL: process.env.NODE_ENV === 'mock' ? `/api/` : '/proxy/',
    baseURL: BaseUrl,
    // 是否跨站点访问控制请求
    withCredentials: false,
    // 返回树状数据大约需要5-6秒，设置最大超时为10秒
    timeout: 10000,
    // 请求数据拦截
    // transformRequest: [
    //     (data) => {
    //         return data
    //     },
    // ],
    validateStatus() {
        // 使用async-await，处理reject情况较为繁琐，所以全部返回resolve，在业务代码中处理异常
        return true
    },
    // 响应数据拦截
    transformResponse: [
        (data) => {
            if (typeof data === 'string' && data.startsWith('{') || data.startsWith('[')) {
                data = JSON.parse(data)
            }
            return data
        },
    ],
});


// 请求拦截器
http.interceptors.request.use(
    (config: AxiosRequestConfig) => {
        console.log("请求拦截器1",config);
        // 使用秘钥加密数据，暂时不做
        // 添加token
        const userInfo = useUserInfo()
        const token = userInfo.gettoken()
        if (userInfo.token) {
            // @ts-ignore
            config.headers['Authorization'] = "jwt " + token
        }
        return config
    },
)


const request = (method: "post" | "get" | "put" | "delete", url: string, data: any = null, params: any = null, headers: any = null, loading: null | Ref<boolean>) => {
    // 判断是否是不防抖的url
    if(!excepturl.includes(url)){
        // 判断是否已经发起请求
        if(antiShakeurlList.includes(url)){
            return Promise.reject("别点这么快，想死啊！")
        }else{
            antiShakeurlList.push(url)
            console.log([...antiShakeurlList])
        }
    }
    const loadingkey: string = url + new Date().getTime()
    if (loading != null) {
        console.log("自定义的loading")
        loading.value = true;
        loadingstorage[loadingkey] = loading
    }else{
        console.log("全局的loading")
        const mylayout = useLayout()
        mylayout.changezhezhao(true)
    }

    const message = (window as any)["$message"]
    return new Promise((resolve, reject) => {
        http({method: method, url: url, data: data, params: params,headers:headers}).then(res => {
            // 请求成功，删除缓存的url
            antiShakeurlList = antiShakeurlList.filter(item=>item!==url)
            if(loadingstorage[loadingkey]){
                loadingstorage[loadingkey].value = false
                delete loadingstorage[loadingkey]
            }else{
                const mylayout = useLayout()
                mylayout.changezhezhao(false)
            }
            if(res.status===200){
                resolve(res)
            }else{
                let result:any = []
                // Object.keys(res.data.error).forEach((item:any)=>{
                //     if(Array.isArray(res.data.error[item])){
                //         result = [...result,...res.data.error[item]]
                //         res.data.error[item].forEach((err1:any)=>{
                //             message.error(err1)
                //         })
                //     }else{
                //         result = [...result,res.data.error[item]]
                //         message.error(res.data.error[item])
                //     }
                // })
                reject(res.data.error)
            }

        }).catch((error: Error) => {
            // 请求失败，删除缓存的url
            antiShakeurlList = antiShakeurlList.filter(item=>item!==url)
            if(loadingstorage[loadingkey]){
                loadingstorage[loadingkey].value = false
                delete loadingstorage[loadingkey]
            }else{
                const mylayout = useLayout()
                mylayout.changezhezhao(false)
            }

            message.error(error.message)
            reject([error.message])
        })
    })
}

// 创建方法
export function json(url: string, data:any = null, loading: null | Ref<boolean> = null) {
    return request(
        "post",
        url,
        data,
        null,
        {'Content-Type': 'application/json;charset=utf-8',},
        loading,
    )
}

// 更新方法
export function update(url: string, data: any = null, id: string | null = null, loading: null | Ref<boolean> = null) {
    if (id) {
        return request(
            "put",
            url + id + "/",
            data,
            null,
            {'Content-Type': 'application/json;charset=utf-8',},
            loading,
        )
    } else {
        return request(
            "put",
            url,
            data,
            null,
            {'Content-Type': 'application/json;charset=utf-8',},
            loading,
        )
    }
}

// 删除方法
export function shanchu(url: string, data:any = null, id:string|null = null, loading: null | Ref<boolean> = null) {
    if (id) {
        return request(
            "delete",
            url + id + "/",
            null,
            data,
            {'Content-Type': 'application/json;charset=utf-8',},
            loading,
        )
    } else {
        return request(
            "delete",
            url,
            null,
            data,
            {'Content-Type': 'application/json;charset=utf-8',},
            loading,
        )
    }
}

// 单查方法
export function get(url: string, data:any = null, id:null|string = null, loading: null | Ref<boolean> =null) {
    if (id) {
        return request(
            "get",
            url + id + "/",
            null,
            data,
            {'Content-Type': 'application/json;charset=utf-8',},
            loading,
        )
    } else {
        return request(
            "get",
            url,
            null,
            data,
            {'Content-Type': 'application/json;charset=utf-8',},
            loading,
        )
    }
}

// 群查方法
export function list(url: string, data:any = null, loading: null | Ref<boolean> = null) {
    return request(
        "get",
        url,
        null,
        data,
        {'Content-Type': 'application/json;charset=utf-8',},
        loading,
    )
}

// 表单创建方法
export function form(url: string, data:any = null, loading: null | Ref<boolean> = null) {
    return request(
        "post",
        url,
        data,
        null,
        {'Content-Type': 'application/x-www-form-urlencoded'},
        // transformRequest: [function (data) {
        //     let ret = ''
        //     for (const it in data) {
        //         ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        //     }
        //     return ret
        // }]
        loading,
    )
}
